#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

static const int maxn=5000;
int m=0, ind[maxn+1], to[2*maxn-1], link[2*maxn-1], dis[maxn+1][maxn+1], ans[maxn+1];

void addSide(int f, int s) {
    m++;
    to[m] = s;
    link[m] = ind[f];
    ind[f] = m;
}

void makeDis(int o, int fa, int s) {
    for (int i=ind[o]; i; i=link[i]) {
        if (to[i]!=fa) {
            dis[s][to[i]] = dis[s][o]+1;
            makeDis(to[i], o, s);
        }
    }
}

int main() {
    freopen("stone.in", "r", stdin);
    freopen("check.out", "w", stdout);

    int n, q;
    scanf("%d", &n);
    for (int i=2; i<=n; i++) {
        int f;
        scanf("%d", &f);
        addSide(f, i);
        addSide(i, f);
    }
    for (int i=1; i<=n; i++) {
        makeDis(i, 0, i);
        ans[i] = n;
    }

#ifdef DEBUG
    int d1=0, d2=0, d3=0;
#endif
    scanf("%d", &q);
    for (int i=1; i<=q; i++) {
        int k;
        scanf("%d", &k);
        static int temp[maxn+1];
        memset(temp, 0, sizeof temp);
        for (int j=1; j<=k; j++) {
            int p;
            scanf("%d", &p);
            for (int x=1; x<=n; x++) {
                temp[x] = max(temp[x], dis[x][p]);
            }
        }
#ifdef DEBUG
        if (temp[1]<ans[1]) {
            d1 = i;
        }
#endif
        for (int x=1; x<=n; x++) {
            ans[x] = min(ans[x], temp[x]);
        }
    }

    for (int i=1; i<=n; i++) printf("%d\n", ans[i]);
#ifdef DEBUG
    printf("D:%d\n", d1);
#endif

    fclose(stdin);
    fclose(stdout);
    return 0;
}
